package net.pantolomin.nayo.api;

import java.io.IOException;
import java.net.InetAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A base events listener with default behaviors for each event
 */
public class AbstractConnectionEventsListener implements ConnectionEventsListener {
	/** The logger */
	private final Logger logger = LoggerFactory.getLogger(getClass());

	public void newConnection(Connection connection) {
		this.logger.info("New connection: {}", connection);
	}

	public void bindDone(InetAddress hostAddress, int port) {
		this.logger.info("Binding done: {}:{}", hostAddress.getHostAddress(), Integer.valueOf(port));
	}

	public void bindFailed(InetAddress hostAddress, int port, String cause) {
		this.logger.error("Failed binding: {}:{} ({})", hostAddress.getHostAddress(), Integer.valueOf(port), cause);
	}

	public void connectionTimeout(InetAddress hostAddress, int port) {
		this.logger.error("Connection timeout: {}:{}", hostAddress.getHostAddress(), Integer.valueOf(port));
	}

	public void connectionFailed(InetAddress hostAddress, int port, String cause) {
		this.logger.error("Failed connection: {}:{} ({})", hostAddress.getHostAddress(), Integer.valueOf(port), cause);
	}

	public void connectionClosed(Connection connection) {
		this.logger.info("Connection closed: {}", connection);
	}

	public void closedByRemote(Connection connection, boolean cleanly) {
		this.logger.info("Remote closed connection ({}): {}", cleanly ? "clean" : "dirty", connection);
	}

	public void dataRead(Connection connection, byte[] data) {
		if (this.logger.isDebugEnabled()) {
			this.logger.debug("Data read (size:{}): {}", Integer.valueOf(data.length), connection);
		}
	}

	public void writeFailed(Connection connection, IOException exception) {
		this.logger.error("Failed to write: {}", connection, exception);
	}

	public void writeBufferFull(Connection connection) {
		this.logger.warn("Write buffer is full: {}", connection);
	}

	public void writeBufferFreed(Connection connection) {
		this.logger.info("Write buffer is free: {}", connection);
	}
}